package com.starblink.store.manage.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.bridge.BridgeUtil
import com.starblink.android.basic.ext.toastSuccess
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.bus.bean.RemoveStoresEvent
import com.starblink.rocketreserver.CancelSubscribeMerchantWebMutation
import com.starblink.rocketreserver.FetchMySubscribeBrandQuery
import com.starblink.rocketreserver.FetchMySubscribeMerchantWebQuery
import com.starblink.rocketreserver.UnsubscribeBrandMutation
import com.starblink.rocketreserver.fragment.BrandVOF
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import kotlinx.coroutines.launch

class ManageStoreVM : BaseViewModel() {
    var stores: MutableLiveData<MutableList<Any>> =
        MutableLiveData(mutableListOf())

    var selectStores: MutableLiveData<MutableList<Any>> =
        MutableLiveData(mutableListOf())

    var pageNo = 1
    var loaded = false

    ///批量取消关注
    fun unFollowStores(isBrandType: Boolean) {
        var ids = arrayListOf<String>()
        selectStores.value!!.forEach {
            if (it is MerchantWebVOF) {
                ids.add(it.id)
            } else if (it is BrandVOF && it.brandId != 0) {
                ids.add(it.brandId.toString())
            }
        }
        viewModelScope.launch {
            if (!isBrandType) {
                val response =
                    SkGqlClient.skMutation(
                        CancelSubscribeMerchantWebMutation(ids = Optional.present(ids)),
                        showDefaultLoading = true
                    )
                loaded = true
                if (response?.data?.cancelSubscribeMerchantWeb == true)
                    dealData(isBrandType, ids)
            } else {
                val response =
                    SkGqlClient.skMutation(
                        UnsubscribeBrandMutation(brandIds = Optional.present(ids)),
                        showDefaultLoading = true
                    )
                loaded = true
                if (response?.data?.unsubscribeBrand == true)
                    dealData(isBrandType, ids)
            }
        }
    }

    private suspend fun dealData(
        isBrandType: Boolean,
        ids: ArrayList<String>
    ) {
        stores.value!!.removeAll(selectStores.value!!)
        stores.value = stores.value
        selectStores.value!!.clear()
        selectStores.value = selectStores.value
        if (stores.value!!.size == 0) {
            pageNo == 1
        }
        val removeStoresEvent = RemoveStoresEvent(ids)
//        if (isBrandType) {
//            FlowBus.with<RemoveStoresEvent>(FlowConst.REMOVE_STORE_EVENT).post(removeStoresEvent)
//        } else {
        FlowBus.with<RemoveStoresEvent>(FlowConst.REMOVE_STORE_EVENT).post(removeStoresEvent)
//        }
        //跨进程发送通知
        BridgeUtil.sendFlowBus(FlowConst.REMOVE_STORE_EVENT, removeStoresEvent)
        toastSuccess("Unfollow Successful")
    }

    fun loadStores(isBrandType: Boolean) {
        if (isBrandType) {    ///获取我订阅的品牌
            viewModelScope.launch {
                val response = SkGqlClient.skQuery(//fetchMySubscribeMerchantWeb
                    FetchMySubscribeBrandQuery(
                        pageNo,
                        30,
                    ), showDefaultLoading = pageNo == 1
                )
                response?.data?.fetchMySubscribeBrand?.subList?.map { it.brandVOF }
                    ?.let { list ->
                        delaData(list)
                    }
            }
        } else {    ///获取我订阅的店铺
            viewModelScope.launch {
                val response = SkGqlClient.skQuery(//fetchMySubscribeMerchantWeb
                    FetchMySubscribeMerchantWebQuery(
                        pageNo,
                        30,
                    ), showDefaultLoading = pageNo == 1
                )
                response?.data?.fetchMySubscribeMerchantWeb?.subList?.map { it.merchantWebVOF }
                    ?.let { list ->
                        delaData(list)
                    }
            }
        }
    }

    private fun delaData(list: List<Any>) {
        stores.value!!.addAll(list)
        stores.value = stores.value
        if (pageNo == 1 && stores.value!!.isEmpty()) {
            loadNoData()
        } else {
            pageNo++
        }
    }

}